<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function createScript(a,b) {
        var script = document.createElement("script");
        script.id = "mini";
        script.onload = function () {
          for (let i = 0, func; (func = b[i++]); ) {
            func();
          }
          b.length = 0
        };
        script.src = "miniConsole.js";
        a.appendChild(script);
      }

      var proxyMiniConsole = (function () {
        var cache = [];
        var handler = function (e) {
          if (e.keyCode == "13") {
            var head = document.querySelector("head");
            if (head.lastElementChild.tagName !== "SCRIPT") {
              createScript(head,cache);
              //   document.body.removeEventListener("keydown", handler, false);
            } else {
              console.log("已有script标签----注册函数");
              const s = document.querySelector("#mini");
              head.removeChild(s);
              createScript(head,cache);
            }
          }
        };

        document.body.addEventListener("keydown", handler, false);

        return {
          log: function () {
            var args = arguments;
            cache.push(function () {
              return miniConsole.log.apply(proxyMiniConsole, args);
            });
          },
          name: "proxyMiniConsole",
        };
      })();

      proxyMiniConsole.log("111");
    </script>
  </body>
</html>
